//
//  TweetDetailView.m
//  Guazi
//
//  Created by koolearn on 11-7-29.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "TweetDetailView.h"
#import "TweetDetailViewController.h"
#import "RemarkViewController.h"
#import "PostViewController.h"
#import "GuaziAppDelegate.h"
#import "QuartzUtils.h"
#import "TweetImageStore.h"
#import "TweetImageViewController.h"
#import "RetweetUsersViewController.h"


@interface TweetDetailView (private)
- (void)toggleSpinner:(BOOL)animation button:(UIButton*)button;
@end

@implementation TweetDetailView

@synthesize status;
@synthesize forwardButton;


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor colorWithRed:0.95f green:0.95f blue:0.95f alpha:1.0f];
        imageButton = nil;
        favoriteFlag = nil;
    }
    return self;
}


- (id) initWithStatus:(Status*)s
{
    self = [super init];
    if ( self ) {
        self.backgroundColor = [UIColor colorWithRed:0.95f green:0.95f blue:0.95f alpha:1.0f];
        self.status = s;
 
        //微博内容
        CGFloat y = 10;
        UIFont* textFont = [UIFont systemFontOfSize:14];
        CGSize size = [status.text sizeWithFont:textFont constrainedToSize:CGSizeMake(300, 480) lineBreakMode:UILineBreakModeCharacterWrap];
        textHeight = size.height;
        CGFloat height = size.height;
        UILabel* text = [[UILabel alloc] initWithFrame:CGRectMake(10, y, 300, height)];
        text.backgroundColor = self.backgroundColor;
        text.lineBreakMode = UILineBreakModeCharacterWrap;
        text.numberOfLines = 200;
        text.text = status.text;
        text.font = textFont;
        [self addSubview:text];
        [text release];
        
        //显示图片
        if ( status.smallPicture && [status.smallPicture length] > 0 ) {
            y = y + height + 5;
            height = 80;
            UIImage* image = [[TweetImageStore sharedInstance] getTweetImage:status.smallPicture square:80.0f delegate:self];
            imageButton = [[UIButton alloc] initWithFrame:CGRectMake( 10, y, 80, 80)];
            [imageButton setImage:image forState:UIControlStateNormal];
            [imageButton addTarget:self action:@selector(showImage) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:imageButton];
        }
        else {
            imageButton = nil;
        }
       
        //timestamp
        y = y + height + 5;
        height = 20;
        UILabel* footer = [[UILabel alloc] initWithFrame:CGRectMake(10, y, 300, height)];
        footer.backgroundColor = self.backgroundColor;
        footer.textColor = [UIColor colorWithRed:0.5f green:0.5f blue:0.5f alpha:1.0f];
        footer.font = [UIFont systemFontOfSize:12];
        footer.textAlignment = UITextAlignmentRight;
        
        NSString *timestamp;
        if ([status.source length]) {
            timestamp = [status.timestamp stringByAppendingFormat:@" 来自%@", [status sourceDescription]];
        }
        else {
            timestamp = status.timestamp;
        }
        footer.text = timestamp;
        
        [self addSubview:footer];
        [footer release];
        
       
        UIColor* buttonTextColor = [UIColor colorWithRed:0 green:0.439f blue:0.753f alpha:1.0f];
        UIColor* buttonClickColor = [UIColor redColor];
        UIColor* buttonShadowColor = [UIColor whiteColor];
        UIFont* buttonFont = [UIFont boldSystemFontOfSize:14];
        
        //转发按钮
        y = y + height + 20;
        buttonY = y;
        height = 20;
        NSString* s = [NSString stringWithFormat:@"转发(%d)", status.forwardCount];
        CGSize sizeofString = [s sizeWithFont:buttonFont];
        CGFloat x = 310 - sizeofString.width;
        self.forwardButton = [[[UIButton alloc] initWithFrame:CGRectMake( x, y, sizeofString.width, height)] autorelease];
        [forwardButton setTitleColor:buttonTextColor forState:UIControlStateNormal];
        [forwardButton setTitleColor:buttonClickColor forState:UIControlStateHighlighted];
        [forwardButton setTitleShadowColor:buttonShadowColor forState:UIControlStateNormal];
        forwardButton.titleLabel.font = buttonFont;  	
        forwardButton.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
        forwardButton.titleLabel.shadowOffset = CGSizeMake(0, 1);
        forwardButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        if ( status.forwardCount > 0 ) {
            [forwardButton addTarget:self action:@selector(retweet) forControlEvents:UIControlEventTouchUpInside];
        }
        
        [forwardButton setTitle:s forState:UIControlStateNormal];
        [self addSubview:forwardButton];
        
        //评论按钮
        s = [NSString stringWithFormat:@"评论(%d)", status.remarkCount];
        sizeofString = [s sizeWithFont:buttonFont];
        x = x - 10 - sizeofString.width;
        commentButton = [[UIButton alloc] initWithFrame:CGRectMake(x, y, sizeofString.width, height)];
        [commentButton setTitleColor:buttonTextColor forState:UIControlStateNormal];
        [commentButton setTitleColor:buttonClickColor forState:UIControlStateHighlighted];
        [commentButton setTitleShadowColor:buttonShadowColor forState:UIControlStateNormal];
        commentButton.titleLabel.font = buttonFont;  	
        commentButton.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
        commentButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        commentButton.titleLabel.shadowOffset = CGSizeMake(0, 1);
        if ( status.remarkCount > 0 ) {
            [commentButton addTarget:self action:@selector(remark) forControlEvents:UIControlEventTouchUpInside];
        }
        
       [commentButton setTitle:s forState:UIControlStateNormal];
        [self addSubview:commentButton];
        
        //收藏按钮
        s = @"收藏";
        sizeofString = [s sizeWithFont:buttonFont];
        x = x - 10 - sizeofString.width;
        favoriteButton = [[UIButton alloc] initWithFrame:CGRectMake(x, y, sizeofString.width, height)];
        [favoriteButton setTitleColor:buttonTextColor forState:UIControlStateNormal];
        [favoriteButton setTitleColor:buttonClickColor forState:UIControlStateHighlighted];
        [favoriteButton setTitleShadowColor:buttonShadowColor forState:UIControlStateNormal];
        favoriteButton.titleLabel.font = buttonFont;  	
        favoriteButton.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
        favoriteButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        favoriteButton.titleLabel.shadowOffset = CGSizeMake(0, 1);
        [favoriteButton setTitle:s forState:UIControlStateNormal];
        [self addSubview:favoriteButton];
        
        x = x - 32 - 2;
        favoriteFlag = [[UIImageView alloc] initWithFrame:CGRectMake(x, y - 6, 32, 32)];
        if ( status.favorited ) {
            favoriteFlag.image = [UIImage imageNamed:@"favorited.png"];
        }
        else {
            favoriteFlag.image = [UIImage imageNamed:@"favorite.png"];
        }
        [self addSubview:favoriteFlag];
    }
    return self;
}


- (void) layoutSubviews
{
    [super layoutSubviews];
    
    UIFont* buttonFont = [UIFont boldSystemFontOfSize:14];
    CGFloat height = 20;
    
    //forwardButton
    NSString* s = forwardButton.titleLabel.text;
    CGSize sizeofString = [s sizeWithFont:buttonFont];
    CGFloat x = 310 - sizeofString.width;
    
    CGRect oldFrame = forwardButton.frame;
    forwardButton.frame = CGRectMake(x, oldFrame.origin.y, sizeofString.width, height);
    
    //commentButton
    s = commentButton.titleLabel.text;
    sizeofString = [s sizeWithFont:buttonFont];
    x = x - 10 - sizeofString.width;
    oldFrame = commentButton.frame;
    commentButton.frame = CGRectMake(x, oldFrame.origin.y, sizeofString.width, height);
    
    //favoriteButton
    s = favoriteButton.titleLabel.text;
    sizeofString = [s sizeWithFont:buttonFont];
    x = x - 10 - sizeofString.width;
    oldFrame = favoriteButton.frame;
    favoriteButton.frame = CGRectMake(x, oldFrame.origin.y, sizeofString.width, height );
    
    //favoriteFlag
    x = x - 34;
    oldFrame = favoriteFlag.frame;
    favoriteFlag.frame = CGRectMake(x, oldFrame.origin.y, 32, 32);
}


- (void) updateStatus:(Status *)sts
{
    self.status = sts;
    NSString* s = [NSString stringWithFormat:@"转发(%d)", status.forwardCount];
    [forwardButton setTitle:s forState:UIControlStateNormal];
    if ( status.forwardCount > 0 ) {
        [forwardButton addTarget:self action:@selector(retweet) forControlEvents:UIControlEventTouchUpInside];
    }
 
    s = [NSString stringWithFormat:@"评论(%d)", status.remarkCount];
    [commentButton setTitle:s forState:UIControlStateNormal];
    if ( status.remarkCount > 0 ) {
        [commentButton addTarget:self action:@selector(remark) forControlEvents:UIControlEventTouchUpInside];
    }
    
    if ( status.favorited ) {
        favoriteFlag.image = [UIImage imageNamed:@"favorited.png"];
    }
    else {
        favoriteFlag.image = [UIImage imageNamed:@"favorite.png"];
    }
    
    [self setNeedsLayout];
}



// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    //分割线
     CGFloat y = textHeight + 40;
     
    if ( status.smallPicture && [status.smallPicture length] > 0 ) {
        y += 80;
    }
    
     CGContextRef context = UIGraphicsGetCurrentContext();
     drawLine( context, 1, 
     [[UIColor colorWithRed:0.835f green:0.835f blue:0.835f alpha:1.0f] CGColor], 
     CGPointMake(0, y), CGPointMake(320, y));
     y++;
     drawLine( context, 1, 
     [[UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:1.0f] CGColor], 
     CGPointMake(0, y), CGPointMake(320, y));
}



- (void)dealloc
{
    [status release];
    [commentButton release];
    [forwardButton release];
    [imageButton release];
    [super dealloc];
}


#pragma mark - tools methods

- (void) retweet
{
    RetweetUsersViewController* controller = [[RetweetUsersViewController alloc] initWithStatus:status];
    [[[GuaziAppDelegate getAppDelegate] currentNavigationController] pushViewController:controller animated:YES];
    [controller release];
}


- (void) remark
{
    RemarkViewController* controller = [[RemarkViewController alloc] initWithStatus:status];
    [[[GuaziAppDelegate getAppDelegate] currentNavigationController] pushViewController:controller animated:YES];
    [controller release];
}


- (void) showImage
{
    TweetImageViewController* controller = [[TweetImageViewController alloc] init];
    controller.imageURL = status.originPicture;
    [[[GuaziAppDelegate getAppDelegate] currentNavigationController] pushViewController:controller animated:YES];
    [controller release];
}


#pragma mark - ImageDownloader Delegate

- (void) imageDidGetNewImage:(UIImage*)image forURL:(NSString*)url
{
    [imageButton setImage:image forState:UIControlStateNormal];
    [imageButton setNeedsDisplay];
}



@end
